package com.neusoft.neuiotms.dcom.repository.hn.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionHealthPlan;
import com.neusoft.neuiotms.dcom.model.hn.QHnInspectionHealthPlan;
import com.neusoft.neuiotms.dcom.repository.hn.CustomizedHnInspectionHealthPlanRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedHnInspectionHealthPlanRepositoryImpl extends AbstractRepository
        implements CustomizedHnInspectionHealthPlanRepository {

    @Override
    public List<HnInspectionHealthPlan> index(final Searchable searchable) {
        final QHnInspectionHealthPlan qPlan = QHnInspectionHealthPlan.hnInspectionHealthPlan;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<HnInspectionHealthPlan> query = this.query().selectFrom(qPlan);

        final Boolean isAll = searchable.getBooleanValue("isAll");
        if ((isAll == null) || !isAll) {
            final List<Long> ids = this.findMaxIdsGroupByCode();
            where.and(qPlan.delFlag.eq(false)).and(qPlan.id.in(ids));
        }
        query.where(where);
        return query.fetch();
    }

    private List<Long> findMaxIdsGroupByCode() {
        final QHnInspectionHealthPlan qPlan = QHnInspectionHealthPlan.hnInspectionHealthPlan;
        final JPAQuery<Long> dquery =
                this.query().select(qPlan.id.max()).from(qPlan)
                        .groupBy(qPlan.code);
        return dquery.fetch();
    }

    @Override
    public Page<HnInspectionHealthPlan> search(final Pageable pageable, final Searchable searchable) {
        final List<Long> ids = this.findMaxIdsGroupByCode();
        final QHnInspectionHealthPlan qPlan = QHnInspectionHealthPlan.hnInspectionHealthPlan;
        final BooleanBuilder where = this.buildWhere(searchable);
        where.and(qPlan.id.in(ids));
        final JPAQuery<HnInspectionHealthPlan> query = this.query().selectFrom(qPlan);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QHnInspectionHealthPlan qPlan = QHnInspectionHealthPlan.hnInspectionHealthPlan;

        if (StringUtils.isNotBlank(searchable.getStrValue("institutionCode"))) {
            where.and(super.equalsStr(qPlan.institutionCode, searchable, "institutionCode"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("institutionName"))) {
            where.and(super.equalsStr(qPlan.institutionName, searchable, "institutionName"));
        }
        // 状态检索
        if (StringUtils.isNotBlank(searchable.getStrValue("disabled"))) {
            where.and(super.equalsBool(qPlan.disabled, searchable, "disabled"));
        }

        //时间检索
        if (StringUtils.isNotBlank(searchable.getStrValue("startDateTimeBegin"))) {
            where.and(
                    super.afterOrEqualsDate(qPlan.startDateTime, searchable, "startDateTimeBegin"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("startDateTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(qPlan.startDateTime, searchable, "startDateTimeEnd"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("endDateTimeBegin"))) {
            where.and(super.afterOrEqualsDate(qPlan.endDateTime, searchable,
                    "endDateTimeBegin"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("endDateTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(qPlan.endDateTime, searchable,
                    "endDateTimeEnd"));
        }
        if (searchable.getBooleanValue("delFlag") != null) {
            where.and(super.equalsBool(qPlan.delFlag, searchable, "delFlag"));
        }

        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return HnInspectionHealthPlan.class;
    }

}
